/* 
 * AUTHOR: Kevin Lam
 */

package com.apps.ubc.cc.ajax;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;

import com.apps.services.AmazonWebService;
import com.apps.services.factory.AmazonWebServiceFactory;
import com.apps.ubc.cc.model.AmazonBookModel;

public class AmazonController extends HttpServlet {
	
	AmazonWebService aws = AmazonWebServiceFactory.getAmazonWebService();
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) {
		String isbn = req.getParameter("isbn");
		AmazonBookModel abm = aws.search(isbn);
		String out = "<book>\n";
		out += "\t<title>"+StringEscapeUtils.escapeXml(abm.getTitle())+"</title>\n";
		out += "\t<image>"+abm.getImageUrl()+"</image>\n";
		out += "\t<detail>"+StringEscapeUtils.escapeXml(abm.getDetailUrl())+"</detail>\n";
		out += "\t<newPrice>"+StringEscapeUtils.escapeXml(abm.getPriceNew())+"</newPrice>\n";
		out += "\t<usedPrice>"+StringEscapeUtils.escapeXml(abm.getPriceUsed())+"</usedPrice>\n";
		out += "</book>";
		try {
			resp.setContentType("text/xml; charset=UTF-8");
			resp.getWriter().write(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
